'''
@Date:2022/10/1  8:18
@Author:yjh
'''
import requests
from tools.logger import log # 导入封装的logger方法


class Common:
    '''封装通用接口'''

    #request-post请求，适用Content-Type:application/x-www-form-urlencoded，即表单传参，类似some=data&xxx=xxx 的形式
    def r_post_form(url,headers,data):
        log.info("请求参数为:{},{}".format(url,data))
        res = requests.post(url,headers=headers,data=data).json()
        log.info("响应结果为:{}".format(res))
        return res

    #request-post请求，适用'Content-Type': 'application/json'，即json传参
    def r_post(url,headers,json):
        log.info("请求参数为:{},{}".format(url, json))
        res = requests.post(url,headers=headers,json=json).json()
        log.info("响应结果为:{}".format(res))
        return res

    # request-post请求，适用'Content-Type': 'application/json'，即json传参——请求时不转换成json，适配s统一登录系统接口请求
    def r_s_post(url, headers, json):
        log.info("请求参数为:{},{}".format(url, json))
        res = requests.post(url, headers=headers, json=json)
        log.info("响应结果为:{}".format(res.text))
        return res

    #request-get请求
    def r_get(url,heasers,json):
        log.info("请求参数为:{},{}".format(url, json))
        res = requests.get(url,headers=heasers,json=json)
        log.info("响应结果为:{}".format(res))
        return res

    #断言：s统一登录系统的message=操作成功
    def assert_s_code_message(res_json):
        code = res_json['code']
        message = res_json['data']
        assert code == '200' and message == '操作成功'

    # 断言：业务系统的message=请求成功
    def assert_tg_code_message(res_json):

        code = res_json['code']
        msg = res_json['msg']
        assert code == '1' and msg == '查询成功'